package com.amat0urheu.service.impl;


import com.amat0urheu.dao.TeamMapper;
import com.amat0urheu.dao.UserMapper;
import com.amat0urheu.pojo.ReturnRes;
import com.amat0urheu.pojo.Team;
import com.amat0urheu.pojo.User;
import com.amat0urheu.pojo.race.Race;
import com.amat0urheu.service.TeamService;
import com.amat0urheu.util.CommonUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.sound.midi.MetaMessage;
import java.util.List;
import java.util.Optional;


@Service
public class TeamServiceImpl implements TeamService {
    final TeamMapper teamMapper;
    final  UserMapper userMapper;
    @Autowired
    public TeamServiceImpl(TeamMapper teamMapper, UserMapper userMapper) {
        this.teamMapper = teamMapper;
        this.userMapper = userMapper;
    }




    @Override
    public ReturnRes getTeamView(int pageSize, int pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        List<Team> teamList = teamMapper.selectAllTeam();
        int rank = pageSize * (pageNum - 1) + 1;
        if (teamList != null) {
            for (Team team: teamList
                 ) {
                List<String>  memberList=teamMapper.getPeoOfTeam(team.getTname());
//                team.et\
                team.setMember(memberList);
                team.setPeopleNum(   teamMapper.getCountOfPeo(team.getTname()));
                team.setRank(rank++);
            }
                return new ReturnRes(true, new PageInfo<>(teamList), "查询成功");
            }
        return new ReturnRes(false, "查询失败");
    }

    @Override
    public ReturnRes addTeam(String tname, String username) {

        if(teamMapper.findNumOfTeam(tname)<4&& (userMapper.haveTeam(username)==0)){
            userMapper.updatePeoTeam(tname,username);
            teamMapper.updateTeamNum(tname);
            return  new ReturnRes(true, "加入成功,请刷新页面");
        }
        return  new ReturnRes(false, "加入失败");


    }

    @Override
    public void reduceTeamNum(String teamName) {
        teamMapper.reduceNum(teamName);
    }

    @Override
    public ReturnRes searchRaceByPageforAdmin(String key, String value, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        String[] whiteList = new String[]{"tcaptain", "tname"};
        if (CommonUtil.strArrayIsHave(whiteList, key)) {
            List<Team> teamList = teamMapper.searchRaceByPageforAdmin(key, "%" + value + "%");

            if (teamList != null) {
                for (Team team: teamList
                ) {
                    List<String>  memberList=teamMapper.getPeoOfTeam(team.getTname());
                    team.setMember(memberList);
                    team.setPeopleNum(   teamMapper.getCountOfPeo(team.getTname()));
                }
                return new ReturnRes(true, new PageInfo<>(teamList), "查询成功");
            } else {
                return new ReturnRes(false, "查询失败");
            }
        } else {
            return new ReturnRes(false, "师傅请勿尝试不安全的参数");
        }
    }
}
